$(function(){
    // 返回购物车
    document.getElementById("returnCart").onclick = function () {
        window.location = "/mi/cart/shopping_cart"
    }


    //进入支付页面
    document.getElementById("gotoPay").onclick = function () {
        // window.location = "mi/buy/order_sub_success?productList"+document.getElementById("productList").value;
        let middler_nav_two = document.getElementsByClassName("middler-nav-two");

        for(let i = 0; i < middler_nav_two.length; i++){
            console.log(middler_nav_two[i])
            if(middler_nav_two[i].getAttribute("index") != "true"){
                // alert(11)
                $('#gotoPay').popover('show')
            }else {
                let indexAddress = parseInt(middler_nav_two[i].children[4].value);
                let paraCartProduct = "";
                let cartIdList = document.getElementsByName("cartIdList");
                for(let i = 0; i < cartIdList.length; i++){
                    paraCartProduct += ":" + cartIdList[i].value;
                }
                paraCartProduct = paraCartProduct.slice(1);
                window.location = "/mi/buy/order_sub_success?paraCartProduct="+paraCartProduct + "&indexAddress=" + indexAddress;
                $('#gotoPay').popover('hide')
            }
                // alert(111);
                // console.log(11)
        }
        // console.log(middler_nav_two);
    }
})


$(function(){
    //模拟一个参数 - 为了post请求,没有参数,默认的是get请求
    $("#outer").load("/mi/cart/chose_address")
})




function addAddress(userId){
    let userName = document.getElementById("userName").value;
    let userPhone = document.getElementById("userPhone").value;
    let userAddress = document.getElementById("userAddress").value;
    let userZipcode = document.getElementById("userZipcode").value;


    let allAddress = document.getElementById("province").value + document.getElementById("city").value + document.getElementById("area").value + userAddress

    // 对用户输入的地址校验
    $.post("/mi/cart/checkAddress",{userId: userId, userName:userName, userPhone:userPhone, userAddress: userAddress, userZipcode:userZipcode},
        function(res){
        // alert(111)
        //     alert(res.code);
        if(res.code==='200'){
            $("#outer").load("/mi/cart/addAddress",{userId: userId, userName:userName, userPhone:userPhone, userAddress: allAddress, userZipcode:userZipcode});
            // alert(2221)
            layer.load(3);
            $('#myModal').modal('hide');
            let c = document.getElementsByClassName("modal-backdrop");
            for(let i = 0; i < c.length; i++){
                c[i].style.width = "0px";
                c[i].style.height = "0px";
                c[i].style.backgroundColor = "rgb(62, 62, 62)";
            }
            layer.closeAll();
        }else if(res.code==='300'){
            // alert(111);
            $('#userName').popover('show')
            // layer.msg(res.msg);
        }else if(res.code==='400'){
            // alert(111);
            document.getElementById("userPhone").setAttribute("data-content", "手机号不能为空")
            $('#userPhone').popover('show')
            // layer.msg(res.msg);
        }
        else if(res.code==='500'){
            // alert(111);
            document.getElementById("userPhone").setAttribute("data-content", "手机号输入格式有误")
            $('#userPhone').popover('show')
            // layer.msg(res.msg);
        }
        else if(res.code==='600'){
            // alert(111);
            $('#userAddress').popover('show')
            // layer.msg(res.msg);
        }
    })
}